import { Node, UIOpacity, Vec3 } from 'cc';
import { ecs } from 'db://app-game/libs/ecs/ECS';
import { StaticModelComp } from './model/StaticModelComp';
import { ViewUtil } from 'db://app-game/core/utils/ViewUtil';
import { StaticCreatureViewComp } from './view/StaticCreatureViewComp';

/** 静态生物实例 */
@ecs.register('StaticCreature')
export class StaticCreature extends ecs.Entity {
  // 数据层
  StaticModel!: StaticModelComp;

  // 视图层
  StaticCreatureView!: StaticCreatureViewComp;
  init() {
    this.addComponents(StaticModelComp);
  }

  /**加载静态生物 */
  load(parent: Node, pos: Vec3) {
    const spriteFrame = this.StaticModel.spriteFrame;
    const node = ViewUtil.createSpriteNode(spriteFrame);
    node.addComponent(UIOpacity);
    const mv = node.addComponent(StaticCreatureViewComp);
    this.add(mv);

    node.setPosition(pos);
    node.parent = parent;
  }

  /** 设置透明 */
  setTransparent(status: boolean) {
    const uiOpacity = this.StaticCreatureView.node.getComponent(UIOpacity);
    if (status) {
      uiOpacity.opacity = 50;
    } else {
      uiOpacity.opacity = 255;
    }
  }
}
